'use client'

import {useState} from 'react'
import type {ProColumns} from '@ant-design/pro-components'
import {ProTable} from '@ant-design/pro-components'
import {Button, message, Modal} from 'antd'
import {PlusOutlined} from '@ant-design/icons'

type PurchaseItem = {
  id: string
  orderNo: string
  supplierName: string
  totalAmount: number
  status: string
  createTime: string
  paymentStatus: string
  remark?: string
}

export default function PurchasePage() {
  const [createModalVisible, setCreateModalVisible] = useState(false)

  const columns: ProColumns<PurchaseItem>[] = [
    {
      title: '采购单号',
      dataIndex: 'orderNo',
      copyable: true,
      width: 200,
    },
    {
      title: '供应商',
      dataIndex: 'supplierName',
      width: 200,
    },
    {
      title: '总金额',
      dataIndex: 'totalAmount',
      width: 120,
      valueType: 'money',
    },
    {
      title: '状态',
      dataIndex: 'status',
      width: 120,
      valueEnum: {
        pending: { text: '待处理', status: 'Default' },
        processing: { text: '处理中', status: 'Processing' },
        completed: { text: '已完成', status: 'Success' },
        cancelled: { text: '已取消', status: 'Error' },
      },
    },
    {
      title: '付款状态',
      dataIndex: 'paymentStatus',
      width: 120,
      valueEnum: {
        unpaid: { text: '未付款', status: 'Error' },
        partial: { text: '部分付款', status: 'Warning' },
        paid: { text: '已付款', status: 'Success' },
      },
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 180,
      valueType: 'dateTime',
    },
    {
      title: '备注',
      dataIndex: 'remark',
      width: 200,
      ellipsis: true,
    },
    {
      title: '操作',
      width: 180,
      key: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a
          key="edit"
          onClick={() => {
            message.info('编辑功能开发中')
          }}
        >
          编辑
        </a>,
        <a
          key="delete"
          onClick={() => {
            Modal.confirm({
              title: '确认删除',
              content: '确定要删除这个采购单吗？',
              onOk: () => {
                message.success('删除成功')
              },
            })
          }}
        >
          删除
        </a>,
      ],
    },
  ]

  const mockData: PurchaseItem[] = [
    {
      id: '1',
      orderNo: 'PO202403010001',
      supplierName: '苹果公司',
      totalAmount: 99999,
      status: 'pending',
      createTime: '2024-03-01 10:00:00',
      paymentStatus: 'unpaid',
      remark: '采购iPhone 15 Pro Max 10台',
    },
    {
      id: '2',
      orderNo: 'PO202403010002',
      supplierName: '华为技术有限公司',
      totalAmount: 89990,
      status: 'completed',
      createTime: '2024-03-01 11:00:00',
      paymentStatus: 'paid',
      remark: '采购华为Mate 60 Pro 10台',
    },
  ]

  return (
    <ProTable<PurchaseItem>
      columns={columns}
      dataSource={mockData}
      rowKey="id"
      search={{
        labelWidth: 120,
      }}
      toolBarRender={() => [
        <Button
          key="button"
          icon={<PlusOutlined />}
          type="primary"
          onClick={() => setCreateModalVisible(true)}
        >
          新建
        </Button>,
      ]}
    />
  )
} 